﻿#region File Header
/***********************************************************************
 * Copyright © 2013 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: February 4th, 2013
Description: TODO: Write a description of this file here.
****************************** Change Log ******************************
02.04.13 - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using GameJolt.Async;
using GameJolt.Requests;
using Newtonsoft.Json;
#endregion

namespace GameJolt
{
	/// <summary>
	/// 
	/// </summary>
	[JsonObject]
	public sealed class Trophy
	{
		#region Properties
		[JsonProperty("id")]
		public string ID
		{
			get;
			set;
		}

		[JsonProperty("title")]
		public string Title
		{
			get;
			set;
		}

		[JsonProperty("description")]
		public string Description
		{
			get;
			set;
		}

		[JsonProperty("difficulty")]
		public string Difficulty
		{
			get;
			set;
		}

		[JsonProperty("image_url")]
		public string Image
		{
			get;
			set;
		}

		[JsonProperty("achieved")]
		public string Achieved
		{
			get;
			set;
		}
		#endregion

		#region Methods
		public static IAsyncResult BeginGetByID(string gameID, string privateKey, string username, string userToken, string trophyID, AsyncCallback callback = null, object asyncState = null)
		{
			var request = TrophyRequestFactory.CreateGetRequest(gameID, privateKey, username, userToken, trophyID);
			return request.Begin(callback, asyncState);
		}

		public static Trophy EndGetByID(IAsyncResult result)
		{
			var joltResult = (AsyncResult<Trophy[]>)result;
			var request = (JsonRequest<Trophy[], TrophyRequestFactory.TrophyResult>)joltResult.CoreData;
			var output = request.End(result);
			return output[0];
		}

		public static Trophy GetByID(string gameID, string privateKey, string username, string userToken, string trophyID)
		{
			var request = TrophyRequestFactory.CreateGetRequest(gameID, privateKey, username, userToken, trophyID);
			return request.Process(null)[0];
		}

		public static IAsyncResult BeginGetAll(string gameID, string privateKey, string username, string userToken, TrophyFilter filter = TrophyFilter.All, AsyncCallback callback = null, object asyncState = null)
		{
			var request = TrophyRequestFactory.CreateGetRequest(gameID, privateKey, username, userToken, filter);
			return request.Begin(callback, asyncState);
		}

		public static Trophy[] EndGetAll(IAsyncResult result)
		{
			var joltResult = (AsyncResult<Trophy[]>)result;
			var request = (JsonRequest<Trophy[], TrophyRequestFactory.TrophyResult>)joltResult.CoreData;
			return request.End(result);
		}

		public static Trophy[] GetAll(string gameID, string privateKey, string username, string userToken, TrophyFilter filter = TrophyFilter.All)
		{
			var request = TrophyRequestFactory.CreateGetRequest(gameID, privateKey, username, userToken, filter);
			return request.Process(null);
		}

		public static IAsyncResult GetAll(string gameID, string privateKey, string username, string userToken, TrophyFilter filter, Action<Trophy[]> callback)
		{
			return BeginGetAll(gameID, privateKey, username, userToken, filter, (result) => callback(EndGetAll(result)), null);
		}

		public static IAsyncResult BeginAddTrophy(string gameID, string privateKey, string username, string userToken, string trophyID, AsyncCallback callback = null, object asyncState = null)
		{
			var request = TrophyRequestFactory.CreateAddAchieveRequest(gameID, privateKey, username, userToken, trophyID);
			return request.Begin(callback, asyncState);
		}

		public static string EndAddTrophy(IAsyncResult result)
		{
			var joltResult = (AsyncResult<string>)result;
			var request = (StringRequest)joltResult.CoreData;
			return request.End(result);
		}

		public static string AddTrophy(string gameID, string privateKey, string username, string userToken, string trophyID)
		{
			var request = TrophyRequestFactory.CreateAddAchieveRequest(gameID, privateKey, username, userToken, trophyID);
			return request.Process(null);
		}

		public static IAsyncResult AddTrophy(string gameID, string privateKey, string username, string userToken, string trophyID, Action<string> callback)
		{
			return BeginAddTrophy(gameID, privateKey, username, userToken, trophyID, (result) => callback(EndAddTrophy(result)));
		}
		#endregion
	}
}
